/*
 * Created on 2006-7-8
 */
package entity;

import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;

public class SameObjectRemover {

	File inFilePath;

	File outFilePath;

	BufferedReader br;

	BufferedWriter bw;

	public SameObjectRemover() {

	}

	public SameObjectRemover(String inFile, String outFile) {
		this.inFilePath = new File(inFile);
		this.outFilePath = new File(outFile);
		try {
			FileReader fr = new FileReader(this.inFilePath);
			this.br = new BufferedReader(fr);
		} catch (FileNotFoundException e) {
			System.out
					.println("In SameObjectRemover(), file not found exception");
			System.exit(-1);
		}

		try {
			FileWriter fw = new FileWriter(this.outFilePath);
			this.bw = new BufferedWriter(fw);
		} catch (IOException e) {
			System.out.println("In SameObjectRemover(), IO exception");
			System.exit(-1);
		}
	}

	public String[] getOneRecord() {
		String line = null;
		try {
			line = this.br.readLine();
			if (line != null) {
				return line.split("\t");
			} else {
				return null;
			}
		} catch (IOException e) {
			System.out.println("in time0process, IOException");
			System.exit(-1);
		}
		return null;
	}

	public void remove() {
		String[] aRecord = this.getOneRecord();
		int curTime = 0;
		while (aRecord != null) {
			Hashtable locations = new Hashtable();

			int readTS = Integer.parseInt(aRecord[4]);
			while (readTS == curTime && aRecord != null) {
				// process a record
				boolean bUnique = false;
				double px = Double.parseDouble(aRecord[5]);
				double py = Double.parseDouble(aRecord[6]);
				float pxf = 0;
				float pyf = 0;

				while (!bUnique) {
					pxf = (float) (px + Math.random() * 100);
					pyf = (float) (py + Math.random() * 100);
					Point2D pLoc = new Point2D.Double(pxf, pyf);
					if (locations.containsKey(pLoc)) {
						System.out.println("same object found, " + aRecord[1]
								+ " " + aRecord[4]);
						pxf = pxf + (float) Math.random() * 100;
						pyf = pyf + (float) Math.random() * 100;
					} else {
						locations.put(pLoc, new Integer(1));
						bUnique = true;
					}
				}
				// write out the record

				aRecord[5] = String.valueOf(pxf);
				aRecord[6] = String.valueOf(pyf);

				String s = aRecord[0];
				for (int i = 1; i < aRecord.length; i++) {
					s = s + "\t" + aRecord[i];
				}

				try {
					this.bw.write(s + "\n");
				} catch (IOException e) {
					System.out.println("In printAllObjects(), IO exception");
					System.exit(-1);
				}

				//
				aRecord = this.getOneRecord();
				if (aRecord != null) {
					readTS = Integer.parseInt(aRecord[4]);
				}
			}
			try {
				this.bw.flush();
			} catch (IOException e) {
				System.out.println("In convert(), IO exception");
				System.exit(-1);
			}
			curTime++;
			System.out.println(curTime + " done");
		}
		System.out.println("done");

	}

	public static void main(String[] args) {
		String Object_inPath = "f:/datasets/forDismknn/O100k_ts20_slow.dat";
		String Object_outPath = "f:/datasets/forDismknn/O100k_ts20_slow_r.dat";
		String usage= "SameObjectRemover inpath outpath";
		if(args.length!=2){
			System.out.println(usage);
		}else{
			Object_inPath=args[0];
			System.out.println(Object_inPath);
			Object_outPath=args[1];
			System.out.println(Object_outPath);
		}
		SameObjectRemover sor = new SameObjectRemover(Object_inPath,
				Object_outPath);
		sor.remove();
	}

}
